package com.westos.echufang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.mysql.cj.util.StringUtils;
import com.westos.echufang.constants.RoleConstants;
import com.westos.echufang.pojo.entity.Admin;
import com.westos.echufang.pojo.entity.Employee;
import com.westos.echufang.service.admin.AdminService;
import com.westos.echufang.service.updatePwd.PwdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CheckPwdController {

    @Autowired
    PwdService pwdService;
    @Autowired
    AdminService adminService;


    @PostMapping("/check1/num")
    @ResponseBody
    public String Check(String oldPwd,String newPwd ){
      //a=1 及oldPwd与数据库password数据相同  2 及oldPwd与数据库password数据不同  3 新旧密码相同 4 新旧密码不同
        String msg="";
        if (StringUtils.isNullOrEmpty(oldPwd)){
            msg="Empty";
        }
        if(StringUtils.isNullOrEmpty(newPwd)){
            msg="Empty";
        }
        if (!StringUtils.isNullOrEmpty(oldPwd)){
                Integer integer = pwdService.checkPwd(oldPwd, newPwd);
                if (integer==1){
                    msg="ok";
                }else if (integer==2){
                    msg="false";
                }
            }
        if (!StringUtils.isNullOrEmpty(newPwd)){
                Integer integer = pwdService.checkPwd(oldPwd, newPwd);
                if (integer==3){
                    msg="false1";
                }else if (integer==4){
                    msg="ok";
                }
            }
        return msg;
    }
    @PostMapping("/check1/update")
    public String updatePwd(String newPwd, Model model){
        //a=0 修改成功  1 修改失败   2  代表newPwd为空
        String s="";
        Integer integer = pwdService.updatePwd(newPwd);
        if(integer==0){
            model.addAttribute("msg","修改成功，请重新登录");
            s="upwdsuccess.html";
        }else if(integer==1){
            model.addAttribute("msg","未知原因修改失败，请重新尝试");
            s="pwd.html";
        }else if(integer==2){
            model.addAttribute("msg","密码为空，请按照正确输入");
            s="pwd.html";
        }
        return s;
    }
    @RequestMapping("/check1/getinfor")
    public String getPersonal(Model model){
        if (RoleConstants.employee!=null){
            Employee emp = adminService.getEmpById();
            model.addAttribute("type","employee");
            model.addAttribute("Employee",emp);
        }
        if(RoleConstants.admin!=null){
            Admin admin = adminService.getPersonalInfor();
            model.addAttribute("type","admin");
            model.addAttribute("Admin",admin);
        }
        return "userinfor.html";
    }

    @RequestMapping("/query/all")
    public String  getAllEmp(Model model,HttpServletRequest request){
        int currentPageNo=1;
        int totalPageCount=0;
        List<Employee> emps = adminService.Emps();
        String pageIndex = request.getParameter("pageIndex");

        if (!StringUtils.isNullOrEmpty(pageIndex)){
            currentPageNo=Integer.valueOf(pageIndex);
        }

        int totalCount=0;
        totalCount=emps.size();
        int pageSize=1;
        if((totalCount%pageSize)==0){
            totalPageCount =(totalCount/pageSize); //总页面
        }else{
            totalPageCount =(totalCount/pageSize)+1;
        }
        if (currentPageNo<0){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<Employee> list = adminService.EmpsByLimit(pageSize, currentPageNo);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("list",list);
        return "allEmp.html";
    }

}
